package com.molichuxing.gateway.bff.user.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.user.convertor.Convert;
import com.molichuxing.gateway.bff.user.entity.request.create.EnterpriseCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.EnterpriseModifyVo;
import com.molichuxing.gateway.bff.user.entity.response.EnterpriseVo;
import com.molichuxing.gateway.bff.user.entity.response.IdentityCardVo;
import com.molichuxing.gateway.bff.user.entity.response.UserVo;
import com.molichuxing.gateway.bff.user.services.UserBffService;
import com.molichuxing.services.business.dto.response.OrderBizDto;
import com.molichuxing.services.business.dto.response.UserBizDto;
import com.molichuxing.services.business.dto.response.UserDetailBizDto;
import com.molichuxing.services.business.service.OrderBizService;
import com.molichuxing.services.business.service.UserBizService;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EnterpriseModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author liyg
 * 2019-08-16
 * 用户bff服务实现类
 * 
 */
@Service("userBffService")
public class UserBffServiceImpl implements UserBffService{

	@Resource
	private UserBizService userBizService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private IdentityCardService identityCardService;
	
	@Resource
	private PhoneRecognitionService phoneRecognitionService;
	
	@Resource
	private FaceRecognitionService faceRecognitionService;
	
	@Resource
	private RegionService regionService;
	
	@Resource
	private BlacklistService blacklistService;
	
	@Resource
	private OrderBizService orderBizService;

	@Resource
	private EnterpriseService enterpriseService;
	
	/**
	 * 
	 * 根据用户id获取用户信息 (包括身份证基本信息)
	 * @param userId
	 * @return 
	 */
	@Override
	public UserVo getUserById(Integer userId) {
		
		UserBizDto  userBizDto= userBizService.getUser(userId);
		
		UserVo userVo = Convert.toUserVo(userBizDto);
		if(null == userVo) {
			return userVo;
		}
		
		PhoneRecognitionDto verifyResult = phoneRecognitionService.getVerifyResult(userId);
		if(null != verifyResult) {
			userVo.setVerifyResult(verifyResult.getVerifyResult());
			userVo.setVerifyTime(DateUtil.formatTime(verifyResult.getVerifyTime()));
		}
		
		/* 黑名单信息*/
		if(userVo.getIsBlack()) {
			BlacklistDto blacklist = blacklistService.getBlacklist(userVo.getPhone());
			if(null != blacklist ) {
				userVo.setReason(blacklist.getReason());
				userVo.setType(blacklist.getType());
			}
			
		}
		
		if(null == userVo.getRegionId()) {
			return userVo;
		}
		RegionInfoDto city= regionService.getCityNameByAreaId(userVo.getRegionId());
		if(null != city) {
			userVo.setRegionName(city.getProvinceFullName()+city.getCityFullName()+city.getAreaFullName());
		}
		return userVo;
	}
	
	/**
	 * 根据订单编号获取用户信息 (包括身份证基本信息)
	 * @param orderCode
	 * @return 
	 */
	@Override
	public UserVo getUserByOrderCode(Long orderCode) {
		
		OrderBizDto order = orderBizService.getOrder(orderCode);
		if(null == order) {
			return null;
		}
		
		UserDetailBizDto userDetailBizDto=userBizService.getUserDetail(order.getUserId());
		UserVo userVo = Convert.toUserVo(userDetailBizDto);
		
		return userVo;
	}
	/**
	 * 获取用户列表
	 *
	 * @param map
	 *            参数
	 * key:name String 用户名 非必填
	 * key:realName String 用户姓名 非必填
	 * key:phone String 用户手机 非必填
	 * key:source Integer 用户来源 非必填
	 * key:startTime String 注册开始时间 非必填
	 * key:endTime String 注册结束时间 非必填
	 * key:isOrder Boolean 是否有订单 非必填
	 * key:isBlack Boolean 是否拉黑 非必填
	 * pageNum Integer 当前页
	 * pageSize Integer 每页显示条数
	 *
	 */
	@Override
	public Paged<UserVo> getUserList(Map<String, Object> map) {
		
		Paged<UserDto> paged = userService.getUserList(map);
		if(null == paged) {
			return new Paged<>();
		}else if(paged.isEmpty()) {
			return new Paged<>(null,paged.getTotal(),paged.getPageSize());
		}
		
		List<UserDto> userDtoList = paged.getList();
		List<UserVo> userVoList = Convert.toUserVoBase(userDtoList);
		
		Integer pageSize=paged.getPageSize();
		Long total=paged.getTotal();
		Paged<UserVo> page=new Paged<>(userVoList,total,pageSize);
		return page;
	}

	/**
	 * 获取用户身份证信息
	 * @param userId
	 * @return
	 */
	@Override
	public IdentityCardVo getIdentityCard(Integer userId) {
		
		IdentityCardDto identityCard = identityCardService.getIdentityCard(userId);
		IdentityCardVo identityCardVo = Convert.toIdentityCardVo(identityCard);
		if(null ==  identityCardVo) {
			return identityCardVo;
		}
		
		FaceRecognitionDto verifyResult = faceRecognitionService.getVerifyResult(identityCard.getIdNumber());
		if(null != verifyResult) {
			identityCardVo.setVerifyResult(verifyResult.getVerifyResult());
			identityCardVo.setVerifyTime(DateUtil.formatTime(verifyResult.getVerifyTime()));
		}
		return identityCardVo;
	}
	
	/**
	 * 获取企业用户列表
	 * 
	 * @param map
	 *            参数
	 * @param key:name
	 *            String 企业名称 非必填
	 * @param key:regionId
	 *            Integer 城市id 非必填
	 *@param  key:startTime
	 *            LocalDateTime 创建开始时间
	 *@param  key:endTime
	 *            LocalDateTime 创建结束时间        
	 *@param  key:sortName   
	 *            String 排序字段 
	 *@param  key:sortWay 
	 *            String 排序方式 
	 * @param key:pageNum
	 *            Integer 当前页
	 * @param key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<EnterpriseVo> getEnterpriseList(Map<String, Object> map) {
		Paged<EnterpriseDto> paged = enterpriseService.getEnterpriseList(map);
		if (null == paged) {
			return new Paged<>();
		} else if (paged.isEmpty()) {
			return new Paged<>(null, paged.getTotal(), paged.getPageSize());
		}
		List<EnterpriseDto> listDto = paged.getList();

		List<Integer> cityIds = new ArrayList<Integer>();
		listDto.forEach(dto -> {
			cityIds.add(dto.getRegionId());
		});
		Map<Integer, RegionInfoDto> regionMap = regionService.getRegionInfoListByIds(cityIds);
		List<EnterpriseVo> listVo = getEnterpriseList(listDto, regionMap);
		Integer pageSize = paged.getPageSize();
		Long total = paged.getTotal();
		Paged<EnterpriseVo> page = new Paged<>(listVo, total, pageSize);
		return page;
	}

	/**
	 * 企业用户数封装
	 *
	 * @param listDto
	 * @param regionMap
	 * @return
	 */
	private List<EnterpriseVo> getEnterpriseList(List<EnterpriseDto> listDto, Map<Integer, RegionInfoDto> regionMap) {
		List<EnterpriseVo> listVo = new ArrayList<>(listDto.size());
		listDto.forEach(dto -> {
			EnterpriseVo vo = new EnterpriseVo();
			BeanUtils.copyProperties(dto, vo);
			if (null != regionMap) {
				RegionInfoDto regionInfoDto = regionMap.get(dto.getRegionId());
				if (null != regionInfoDto) {
					vo.setRegionName(regionInfoDto.getCityName());
					vo.setProvinceId(regionInfoDto.getProvinceId());
					vo.setProvinceName(regionInfoDto.getProvinceName());
				}
			}
			listVo.add(vo);
		});
		return listVo;
	}
	
	/**
	 * 获取企业用户详情信息
	 * @param id
	 * @return
	 */
	@Override
	public EnterpriseVo getEnterpriseById(Integer id) {
		EnterpriseDto dto = enterpriseService.getEnterprise(id);
		if (null == dto) {
			return null;
		}
		EnterpriseVo vo = new EnterpriseVo();
		BeanUtils.copyProperties(dto, vo);
		RegionInfoDto regionInfoDto = regionService.getCityNameByCityId(dto.getRegionId());
		if (null != regionInfoDto) {
			vo.setRegionName(regionInfoDto.getCityName());
			vo.setProvinceId(regionInfoDto.getProvinceId());
			vo.setProvinceName(regionInfoDto.getProvinceName());
		}
		return vo;
	}
	
	/**
	 * 新增企业用户信息
	 * @param createVo
	 * @return
	 */
	@Override
	public Boolean createEnterprise(EnterpriseCreateVo createVo) {
		EnterpriseCreateDto createDto = new EnterpriseCreateDto();
		BeanUtils.copyProperties(createVo, createDto);
		return enterpriseService.create(createDto);
	}
	
	/**
	 * 修改企业用户信息
	 * @param modifyVo
	 * @return
	 */
	@Override
	public Boolean modifyEnterprise(EnterpriseModifyVo modifyVo) {
		EnterpriseModifyDto modifyDto = new EnterpriseModifyDto();
		BeanUtils.copyProperties(modifyVo, modifyDto);
		return enterpriseService.Modify(modifyDto);
	}
	
	/**
	 * 模糊获取企业用户信息列表
	 * @param name
	 * @return
	 */
	@Override
	public List<EnterpriseVo> getEnterpriseBaseList(String name) {
		List<EnterpriseDto> list = enterpriseService.getEnterpriseFuzzyByName(name);
		return Convert.toEnterpriseVo(list);
	}
}
